﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlueMarineWebClient.BlueMarineService;
using BlueMarineWebClient.DO;

namespace BlueMarineWebClient.Admin
{
    public partial class Default : Page
    {
        protected void btnLoadPro_Click(object sender, EventArgs e)
        {
            try
            {
                List<BlueMarineService.vSubPromotion> listPromotion = ServiceBool.ExService.PromotionsGet(true);
                if (listPromotion.Count == 0)
                    lblResult.Text = "No Promotion";
                else
                {
                    dlPromotion.DataSource = listPromotion;
                    dlPromotion.DataBind();
                    lblResult.Text = "Load Complete";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = "ERROR" + ex.Message;
            }
        }

        protected void btnGetProImage_Click(object sender, EventArgs e)
        {
            try
            {
                List<BlueMarineService.Promotion> listPromotion = ServiceBool.ExService.GetListPromotion();
                if (listPromotion.Count == 0)
                    lblResult.Text = "No Promotion to Load Image";
                else
                {
                    foreach (BlueMarineService.Promotion pro in listPromotion)
                    {
                        byte[] bytedata = pro.PromotionBanner;
                        string filename = @"../images/promotion/" + pro.PromotionID.ToString() + ".jpg";
                        var FS = new FileStream(Server.MapPath(filename), FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        FS.Write(bytedata, 0, bytedata.Length);
                        FS.Close();
                        GetProDetailImage(pro.PromotionID);
                    }

                    lblResult.Text = "Load All Images Complete";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = "ERROR" + ex.Message;
            }
        }

        protected void dlPromotion_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var lblProID = (Label) dlPromotion.SelectedItem.FindControl("lblProID");
                int proID = int.Parse(lblProID.Text);
                List<vSubPromotionDetail> listPromotionDetail = ServiceBool.ExService.PromotionDetailsGetByPromotionId(proID);
                if (listPromotionDetail.Count == 0)
                    lblResult.Text = "No Promotion Detail";
                else
                {
                    dlPromotionDetail.DataSource = listPromotionDetail;
                    dlPromotionDetail.DataBind();
                    lblResult.Text = "Load Promotion Detail Complete";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = "ERROR: " + ex.Message;
            }
        }

        protected void GetProDetailImage(int proID)
        {
            try
            {
                List<PromotionDetail> listPromotionDetail = ServiceBool.ExService.GetPromotionDetails(proID);
                if (listPromotionDetail.Count == 0) return;
                foreach (PromotionDetail pro in listPromotionDetail)
                {
                    byte[] bytedata = pro.PromotionImage;
                    string filename = @"../images/promotiondetail/" + pro.PromotionDetailID.ToString() + ".jpg";
                    var FS = new FileStream(Server.MapPath(filename), FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    FS.Write(bytedata, 0, bytedata.Length);
                    FS.Close();
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = "ERROR" + ex.Message;
            }
        }
    }
}